package com.dazhi.show.core;

import com.dazhi.show.config.MyConfig;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@RestController
@RequestMapping("api/upload")
public class UploadFileCacheController {
    @Resource
    private MyConfig myConfig;

    @PostMapping("/cache")
    public Result<String> uploadFileCache(MultipartFile file){
        // 将文件保存到文件夹
        String filename = "";
        try {
            File fileFolder = new File(myConfig.tempFolder);

            Path path = fileFolder.toPath();
            filename = file.getOriginalFilename();
            File f = path.resolve(filename).toFile();
            FileCopyUtils.copy(file.getBytes(), f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result<>(200, "success", filename);
    }
}
